	#include<iostream>
	#include<cstdio>
	#include<map>
	#include<queue>
	#include<cstring>
	using namespace std;
	int n;
	struct node{
	int x;
	int len;
	bool v;
	};
	int main(){
	while(scanf("%d", &n) != EOF){
	int a[120][120];
	string s;
	map<string, int> m;
	cin>>s;
	int k = 0;
	memset(a, 0, sizeof(a));
	int in[120];
	for (int i = 1; i <= n; i++){
	string s1, s2, s3;
	cin>>s1>>s2>>s3;
	int x = 0, y = 0, z = 0;
	if(m.find(s1) == m.end()){
	k++;
	m[s1] = k;
	}
	x = m[s1];
	if(s2 == "null"){
	}else{
	if(m.find(s2) == m.end()){
	k++;
	m[s2] = k;
	}
	y = m[s2];
	if(a[y][x] == 0) in[x]++;
	a[y][x] = 1;
	}
	if(s3 == "null"){
	}else{
	if(m.find(s3) == m.end()){
	k++;
	m[s3] = k;
	}
	z = m[s3];
	if(a[x][z] == 0) in[z]++;
	a[x][z] = 1;
	}
	}
	int Po = m[s];
	queue<node> q;
	int d[120];
	memset(d, 0, sizeof(d));
	for (int i = 1; i <= k; i++){
	if(in[i] == 0){
	cout<<16555<<endl;
	node Node;
	Node.x = i;
	Node.len = 1;
	if(i == Po) Node.v = 1;
	else Node.v = 0;
	q.push(Node);
	d[i] = 1;
	}
	}
	int ans = 0;
	while(!q.empty()){
	node Node;
	Node = q.front();
	if(Node.v == 1){
	ans = max(ans, Node.len);
	}
	q.pop();
	for (int i = 1; i <= k; i++){
	if(a[Node.x][i] && in[i] > 0){
	in[i]--;
	d[i] = max(d[i], Node.len + 1);
	if(in[i] == 0){
	node Next;
	Next.x = i;
	Next.len = d[i];
	if(i == Po) Next.v = 1;
	else Next.v = Node.v;
	q.push(Next);
	}
	}
	}
	}
	printf("%d\n", ans);
	}
	}
	
